package cn.iocoder.yudao.module.dify.config;

import io.github.imfangs.dify.client.DifyClientFactory;
import io.github.imfangs.dify.client.DifyWorkflowClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class DifyWorkflowManager {

    @Value("${spring.dify.baseUrl}")
    private String baseUrl;

    private final Map<String, String> workflowKeys = new HashMap<>();
    private final Map<String, DifyWorkflowClient> workflowClients = new ConcurrentHashMap<>();

    @PostConstruct
    public void init() {
        // 可以从配置文件加载工作流键值对
        loadWorkflowKeys();
        initializeClients();
    }

    private void loadWorkflowKeys() {
        // 从配置或数据库加载工作流配置
        workflowKeys.put("translate", "app-0fHAaTY1lr8kzCjSWJ3rCKq5");//翻译工作流
        workflowKeys.put("translate2", "app-JLaZHWTF5wbYCpkD4egMjhuY");//翻译工作流最新
        workflowKeys.put("translate3", "app-e2lmNbtqx9DGbQzmmaHBPmoc");//翻译工作流,在java保留格式
//        workflowKeys.put("summary", "summary-workflow-key");
//        workflowKeys.put("analysis", "analysis-workflow-key");
    }

    private void initializeClients() {
        workflowKeys.forEach((name, key) ->
                workflowClients.put(name, DifyClientFactory.createWorkflowClient(baseUrl, key))
        );
    }

    public DifyWorkflowClient getClient(String workflowName) {
        return workflowClients.get(workflowName);
    }

    public Set<String> getAvailableWorkflows() {
        return workflowClients.keySet();
    }
}

